package org.example.bulletin.controller;


import org.example.entity.Base.Result;
import org.example.bulletin.entity.dto.Bulletindto;
import org.example.bulletin.entity.vo.Bulletinvo;
import org.example.bulletin.service.BulletinService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/bulletin")
public class BulletinController {
    private final BulletinService bulletinService;

    public BulletinController(BulletinService bulletinService) {
        this.bulletinService = bulletinService;
    }

    @GetMapping("/list")
    public Result<PageInfo> List(@RequestParam(name="PageNumber", defaultValue = "1")int PageNumber, @RequestParam(name="PageSize", defaultValue = "5")int PageSize){
        System.out.println(PageNumber+"|||||"+PageSize);
        return Result.success(bulletinService.getList(PageNumber,PageSize));
    }
    @GetMapping("/bulletinl")
    public List bulletinl(){
        return bulletinService.bulletinl();
    }
    @GetMapping("/topList")
    public Result TopList(){
        List<Bulletinvo> BulletinList=bulletinService.getTopList();
        if (BulletinList==null){
            return Result.error("无查询到公告，查询失败");
        }
        return  Result.success(BulletinList);
    }
    @PostMapping("/addBulletin")
    public Result addBulletin(@RequestParam(name="title")String title, @RequestParam(name="top") Integer top, @RequestParam(name="content") String content,@RequestParam(name="guserName") String guserName){
    try {
            Bulletindto bulletindto=new Bulletindto(title,top,guserName,content);
            bulletinService.addBulletin(bulletindto);
            return Result.success();
    }
    catch (Exception e){
        return Result.error("添加失败");
    }
    }
    @PutMapping("/updateBulletin")
    public Result updateBulletin( @RequestParam(name="id") Integer id,@RequestParam(name="title")String title
            , @RequestParam(name="createTime") String createTime, @RequestParam(name="top") Integer top
            , @RequestParam(name="guserName") String guserName, @RequestParam(name="content") String content){
        try {
            Bulletindto bulletindto=new Bulletindto(id,title,createTime,top,guserName,content);
            bulletinService.updateBulletin(bulletindto);
            return Result.success();
        }
        catch (Exception e){
            return Result.error("修改失败");
        }
    }
    @PostMapping("/deleteBulletin")
    public Result deleteBulletin(@RequestBody Bulletindto bulletindto){
        try {
            bulletinService.deleteBulletin(bulletindto);
            return Result.success();
        }
        catch (Exception e){
            return Result.error("删除失败");
        }
    }
    @GetMapping("/getBulletin")
    public Result getBulletin(Integer id){
        try {
            Bulletinvo bulletinvo = bulletinService.getBulletin(id);
            return Result.success(bulletinvo);
        }
        catch (Exception e){
            return Result.error("查询失败");
        }
    }
}
